﻿using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Web;

namespace GrainManage.Server.Util
{
    public sealed class IPUtil
    {

        public static string GetLocalIP()
        {

            //本机IP地址  

            string strLocalIP = null;

            //得到计算机名  

            string hostName = Dns.GetHostName();

            //得到本机IP地址数组  

            IPHostEntry ipEntry = Dns.GetHostEntry(hostName);

            //遍历数组  

            foreach (var ipAddress in ipEntry.AddressList)
            {

                //判断当前字符串是否为正确IP地址  

                if (IsRightIP(ipAddress.ToString()))
                {

                    //得到本地IP地址  

                    strLocalIP = ipAddress.ToString();

                    //结束循环  

                    break;

                }

            }

            //返回本地IP地址  

            return strLocalIP;

        }

        public static string GetGateway()
        {

            //网关地址  

            string strGateway = "";

            //获取所有网卡  

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            //遍历数组  

            foreach (var netWork in nics)
            {
                //单个网卡的IP对象  

                IPInterfaceProperties ip = netWork.GetIPProperties();

                //获取该IP对象的网关  

                GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;

                foreach (var gateWay in gateways)
                {

                    //如果能够Ping通网关  

                    if (IsPingIP(gateWay.Address.ToString()))
                    {

                        //得到网关地址  

                        strGateway = gateWay.Address.ToString();

                        //跳出循环  

                        break;

                    }

                }



                //如果已经得到网关地址  

                if (strGateway.Length > 0)
                {

                    //跳出循环  

                    break;

                }

            }



            //返回网关地址  

            return strGateway;

        }

        public static string RequestHostAddress
        {
            get
            {
                var request = HttpContext.Current.Request;
                string result = request.ServerVariables["REMOTE_ADDR"];
                if (string.IsNullOrEmpty(result))
                {
                    result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = request.UserHostAddress;
                }
                if (string.IsNullOrEmpty(result) || !IsIP(result))
                    return "127.0.0.1";
                return result;
            }
        }

        public static bool IsRightIP(string ip)
        {
            //利用正则表达式判断字符串是否符合IPv4格式  
            return Regex.IsMatch(ip, "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$");
        }

        public static bool IsPingIP(string ip)
        {

            //创建Ping对象  

            Ping ping = new Ping();

            //接受Ping返回值  

            PingReply reply = ping.Send(ip, 1000);

            return reply != null ? true : false;

        }

        public static bool IsIP(string ip)
        {
            // 是否为ip
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
    }
}